package com.lujianfei.business.drawobject;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import com.lujianfei.business.base.OnDrawListener;
import com.lujianfei.business.beans.Location;
import com.lujianfei.business.beans.Person;
import com.lujianfei.business.beans.Shop;
import com.lujianfei.business.beans.SuperMarket;
import com.lujianfei.business.utils.RandomHelper;

public class MainLogicDrawObject implements OnDrawListener {

	List<Person> mPersons = new ArrayList<Person>();
	List<Shop> mShops = new ArrayList<Shop>();

	SuperMarket mSuperMarket = new SuperMarket();

	private MainLogicDrawObject() {
	}

	@Override
	public void paint(Graphics g) {
		drawMarket(g);
		drawPersons(g);
		drawShops(g);
	}

	private void drawMarket(Graphics g) {
		// TODO Auto-generated method stub
		mSuperMarket.paint(g);
	}

	private void drawPersons(Graphics g) {
		for (Person person : mPersons) {
			person.paint(g);
		}
	}
	
	private void drawShops(Graphics g) {
		for (Shop shop : mShops) {
			shop.paint(g);
		}
	}

	@Override
	public void update() {
		for (Person person : mPersons) {
			person.getLocation().x += 1;
		}
	}

	private void initPersons() {
		Point center = new Point(mSuperMarket.getLocation().x + mSuperMarket.getWidth() / 4, mSuperMarket.getLocation().y + mSuperMarket.getHeight() / 2); // Center of person start location
		for (int i = 0; i < 100; i++) {
			Person e = new Person();
			// 产生N(a,b)的数：Math.sqrt(b)*random.nextGaussian()+a
			int x = (int) (50 * RandomHelper.getInstance().gaussion() + center.x);
			int y = (int) (50 * RandomHelper.getInstance().gaussion() + center.y);
			if (x < mSuperMarket.getLocation().x) {
				x = mSuperMarket.getLocation().x + 1;
			}
			if (x > mSuperMarket.getLocation().x + mSuperMarket.getWidth()) {
				x = mSuperMarket.getLocation().x + mSuperMarket.getWidth() - 1;
			}
			if (y < mSuperMarket.getLocation().y) {
				y = mSuperMarket.getLocation().y + 1;
			}
			if (y > mSuperMarket.getLocation().y + mSuperMarket.getHeight()) {
				y = mSuperMarket.getLocation().y + mSuperMarket.getHeight() - 1;
			}
			Location location = new Location(
					x,y);
			e.setLocation(location);
			mPersons.add(e);
		}
	}
	
	private void initShops() {
		Location firstLocation = new Location(mSuperMarket.getLocation());
		Location lastLocation = new Location(firstLocation);
		String[] shopNames = new String[] {"星巴克" , "奈雪の茶", "Yo-Tea", "喜茶", "85度°c"};
		int width = mSuperMarket.getWidth()/5;
		int height = mSuperMarket.getHeight()/4;
		for (int i = 0; i < 5; i++) {
			if (0 == i) {
				Shop e = new Shop();
				e.setLocation(new Location(lastLocation));
				e.setWidth(width);
				e.setHeight(height);
				e.setName(shopNames[i]);
				mShops.add(e);
			} else {
				Shop e = new Shop();
				lastLocation.x += width;
				e.setLocation(new Location(lastLocation));
				e.setWidth(width);
				e.setHeight(height);
				e.setName(shopNames[i]);
				mShops.add(e);
			}
		}
	}

	public static class Builder {

		MainLogicDrawObject mMainLogicDrawObject = new MainLogicDrawObject();

		public Builder setSuperMarket(Location location, int width, int height) {
			mMainLogicDrawObject.mSuperMarket.setLocation(location);
			mMainLogicDrawObject.mSuperMarket.setWidth(width);
			mMainLogicDrawObject.mSuperMarket.setHeight(height);
			mMainLogicDrawObject.initShops();
			mMainLogicDrawObject.initPersons();
			return this;
		}

		public MainLogicDrawObject create() {
			return mMainLogicDrawObject;
		}
	}
}
